/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-4 下午5:00
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.adapter.MyInfoAdapter;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.bean.MyInfo;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.recycleview.ConsulationRecycleViewLayoutManager;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:我的消息Activity
 * autour:guanluocang
 * date:17/7/4 17:03
 */
public class MyInfoActivity extends BaseActivity {

    private Context context;
    @Bind(R.id.rv_info)
    RecyclerView rv_info;
    private List<MyInfo.DataBean> myInfoList = new ArrayList<>();
    private MyInfoAdapter myInfoAdapters;


    @Override
    public void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_info);
        context = this;
        ButterKnife.bind(this);
        initTitleBar(R.string.txt_myInfo);
        setRightTextVisibility(View.GONE);
    }

    @Override
    public void initData() {
        myInfoAdapters = new MyInfoAdapter(context);
        rv_info.setAdapter(myInfoAdapters);
        ConsulationRecycleViewLayoutManager layoutManager = new ConsulationRecycleViewLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_info.setLayoutManager(layoutManager);
        rv_info.setItemAnimator(new DefaultItemAnimator());
        rv_info.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        doData();
    }

    private void doData() {
        RequestParams params = new RequestParams(Config.my_info);
        params.addBodyParameter("id_auth", user.getId_auth());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 200) {
                    MyInfo myInfo = new Gson().fromJson(result, MyInfo.class);
                    myInfoList = myInfo.getData();
                    myInfoAdapters.refresh_info(myInfoList);
                    myInfoAdapters.setOnItemClickListener(new MyInfoAdapter.onItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Bundle bundle = new Bundle();
                            bundle.putString("info_id",myInfoList.get(position).getId());
                            JumpItent.jump(MyInfoActivity.this,MyInfoDeatilActivity.class,bundle);
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
